package co.fitstart.admin.web.controller.system;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

import com.demo2do.core.BusinessException;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;


/**
 * @author mohanlan
 */
@Controller
@RequestMapping("/principals")
@MenuSetting("menu-system")
public class PrincipalCreateController {
    
    private static Logger logger = LoggerFactory.getLogger(PrincipalCreateController.class);
    
    @Autowired
    private PrincipalService principalService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Site> dataService;
    
    /**
     * display create normal panel
     * 
     * @return
     */
    @RequestMapping(value = "/create-normal-principal", method = RequestMethod.GET)
    public String createNormal() {
        return "system/principal/normal-principal-create";
    }
    
    /**
     * create a new principal with managed sites
     * 
     * @param principal
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params = "create-type=NORMAL")
    public @ResponseBody String onCreateNormal(Principal principal) {
        
        Result result = new Result();
        
        try {
            
            principalService.create(principal);
            
            result.success().message("创建用户成功！");
            
        } catch(BusinessException e) {
            
            result.fail().message(e.getMessage());
            
        } catch(Exception e) {
            
            logger.error("#onCreateNormal()", e);
            
            result.fail().message("创建用户失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    
    }
    
    /**
     * display create coach panel
     * 
     * @return
     */
    @RequestMapping(value = "/create-coach", method = RequestMethod.GET)
    public String createCoach() {
        return "system/principal/coach-create";
    }
    
    /**
     * 
     * @param principal
     * @param weixinid
     * @param managerId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params = "create-type=COACH")
    public @ResponseBody String onCreateCoach(Principal principal,
                                              @RequestParam("weixinid") String weixinid,
                                              @RequestParam(value = "managerId", required = true) Long managerId) {

        Result result = new Result();
        
        try {
            
            Principal manager = principalService.load(Principal.class, managerId);
            
            principalService.createCoach(principal, weixinid, manager);
            
            result.success().message("创建教练成功！");
            
        } catch(BusinessException e) {
            
            result.fail().message(e.getMessage());
            
        } catch(Exception e) {
            
            logger.error("#onCreateCoach()", e);
            
            result.fail().message("创建教练失败！");
        }
        
        return JsonUtils.toJsonString(result);
    
    }
    
    /**
     * display create site service panel
     * 
     * @return
     */
    @RequestMapping(value = "/create-site-service", method = RequestMethod.GET)
    public String createSiteService() {
        return "system/principal/site-service-create";
    }
    
    /**
     * create a new site service with relation ( manager & site )
     * 
     * @param principal
     * @param siteId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params = "create-type=SITE_SERVICE")
    public @ResponseBody String onCreateSiteAssist(Principal principal,
                                                   @RequestParam(value = "siteId", required = true) Long siteId) {

        Result result = new Result();
        
        try {
            
            Site site = dataService.load(Site.class, siteId);
            
            principalService.createSiteAssist(principal, site);
            
            result.success().message("创建门店客服成功！");
            
        } catch(BusinessException e) {
            
            result.fail().message(e.getMessage());
            
        } catch(Exception e) {
            
            logger.error("#onCreateCoach()", e);
            
            result.fail().message("创建门店客服失败！");
        }
        
        return JsonUtils.toJsonString(result);
    
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create-area-manager", method = RequestMethod.GET)
    public String createAreaManager() {
        return "system/principal/area-manager-create";
    }
    
    /**
     * 
     * @param principal
     * @param siteIds
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params = "create-type=AREA_MANAGER")
    public @ResponseBody String onCreateAreaManager(Principal principal,
                                                    @RequestParam("siteIds") Long[] siteIds) {

        Result result = new Result();
        
        try {
            List<Site> sites = new ArrayList<>();
            
            for(Long siteId : siteIds) {
                sites.add(dataService.load(Site.class, siteId));
            }
            
            principalService.createAreaManager(principal, sites);
            
            result.success().message("创建大店长成功！");
        } catch(Exception e) {
            logger.error("#onCreateAreaManager()", e);
            
            result.fail().message("创建大店长失败！");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create-area-server", method = RequestMethod.GET)
    public String createAreaServer() {
        return "system/principal/area-server-create";
    }
    
    /**
     * 
     * @param principal
     * @param siteIds
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params = "create-type=AREA_SERVER")
    public @ResponseBody String onCreateAreaServer(Principal principal,
                                                   @RequestParam("siteIds") Long[] siteIds) {

        Result result = new Result();
        
        try {
            List<Site> sites = new ArrayList<>();
            
            for(Long siteId : siteIds) {
                sites.add(dataService.load(Site.class, siteId));
            }
            
            principalService.createAreaServer(principal, sites);
            
            result.success().message("创建运营主管成功！");
        } catch(Exception e) {
            logger.error("#onCreateAreaServer()", e);
            
            result.fail().message("创建运营主管失败！");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create-site-manager", method = RequestMethod.GET)
    public String createSiteManager() {
        return "system/principal/site-manager-create";
    }
    
    /**
     * 
     * @param principal
     * @param siteId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params = "create-type=SITE_MANAGER")
    public @ResponseBody String onCreateSiteManager(Principal principal,
                                                    @RequestParam("siteId") Long siteId) {

        Result result = new Result();
        
        try {
            Site site = dataService.load(Site.class, siteId);
            
            principalService.createSiteManager(principal, site);
            
            result.success().message("创建店长成功！");
        } catch(BusinessException e) {
            result.fail().message(e.getMessage());
        } catch(Exception e) {
            logger.error("#onCreateSiteManager()", e);
            
            result.fail().message("创建店长失败！");
        }
        
        return JsonUtils.toJsonString(result);
    }
}